#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

vector<vector<int>> updateMatrix(vector<vector<int>> &mat)
{
    int m = mat.size();
    int n = mat[0].size();
    vector<vector<int>> dp(m, vector<int>(n, INT_MAX));
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (mat[i][j] == 0)
                dp[i][j] = 0;
            else
            {
                if (j > 0)
                {
                    dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
                }
                if (i > 0)
                {
                    dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
                }
            }
        }
    }

    for (int i = m - 1; i >= 0; i--)
    {
        for (int j = n - 1; j >= 0; j--)
        {
            if (mat[i][j] != 0)
            {
                if (j < n - 1)
                {
                    dp[i][j] = min(dp[i][j], dp[i][j + 1] + 1);
                }
                if (i < m - 1)
                {
                    dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1);
                }
            }
        }
    }
    return dp;
}



int main()
{
    vector<vector<int>> gird{{0, 0, 0}, {0, 1, 0}, {1, 1, 1}};
    vector<vector<int>> dp;
    
    dp=updateMatrix(gird);
    for (int i = 0; i < gird.size(); i++)
    {
        for (int j = 0; j < gird[0].size(); j++)
        {
            cout << dp[i][j]<<" ";
        }
        cout <<endl;
    }
    return 0;
}